Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor theme fonts class for readability #661

Merged
merged 7 commits into from
Jun 4, 2024

Conversation

vcanales
Copy link
Member

What?

This Pull Request refactors the them fonts class to improve code readability and maintainability. The main focus is on cleaning up the code.

I felt inclined to do this because when debugging for #660, I found some parts of the code hard to follow.

Why?

This PR addresses the need to make the code more readable and maintainable by:

  1. Adding documentation and comments.
  2. Simplifying functions and improving their readability.

How?

Key Changes:

  1. Class documentation added: Added descriptions and the purpose of the CBT_Theme_Fonts class.

  2. Function improvements:

    • Inline Descriptions/Justifications:
      • Refactored make_theme_font_src_absolute() to use array_map for cleaner array handling.
      • Replaced double newlines and improved spacing for better readability.
    • Changed foreach to array_map: This reduces the complexity and makes the handling of arrays cleaner.
  3. Code clean-up:

    • Removed some redundant comments in favor of "self documenting" practices.
    • Used shorthand assignment (?? null) for checking array keys.
    • Introduced variables for commonly used expressions.
  4. Function calls:

    • Replaced CBT_Theme_Fonts:: with self:: for static method calls within the class, enhancing readability and context awareness.
  5. Added test cases for parts of the code that could break on further refactors.

Copy link
Contributor

@matiasbenedetto matiasbenedetto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably we should use this refactor to improve the tests of this class. They currently relay on setting users, creating new themes dynamically and after removing it without restoring the previous state.

Example:

public function test_copy_activated_fonts_to_theme() {
wp_set_current_user( self::$admin_id );
$user_data_begin = CBT_Theme_JSON_Resolver::get_user_data()->get_settings();
$test_theme_slug = $this->create_blank_theme();
$this->activate_user_font();
$user_data_before = CBT_Theme_JSON_Resolver::get_user_data()->get_settings();
$theme_data_before = CBT_Theme_JSON_Resolver::get_theme_data()->get_settings();
$this->save_theme();
$user_data_after = CBT_Theme_JSON_Resolver::get_user_data()->get_settings();
$theme_data_after = CBT_Theme_JSON_Resolver::get_theme_data()->get_settings();
// ensure that the font was added and then removed from user space
$this->assertarraynothaskey( 'typography', $user_data_begin );
$this->assertequals( 'open-sans', $user_data_before['typography']['fontFamilies']['custom'][0]['slug'] );
$this->assertarraynothaskey( 'typography', $user_data_after );
// Ensure that the font was added to the theme
$this->assertCount( 1, $theme_data_before['typography']['fontFamilies']['theme'] );
$this->assertCount( 2, $theme_data_after['typography']['fontFamilies']['theme'] );
$this->assertEquals( 'open-sans', $theme_data_after['typography']['fontFamilies']['theme'][1]['slug'] );
// Ensure that the URL was changed to a local file and that it was copied to where it should be
$this->assertEquals( 'file:./assets/fonts/open-sans-normal-400.ttf', $theme_data_after['typography']['fontFamilies']['theme'][1]['fontFace'][0]['src'][0] );
$this->assertTrue( file_exists( get_stylesheet_directory() . '/assets/fonts/open-sans-normal-400.ttf' ) );
$this->uninstall_theme( $test_theme_slug );
}

I think this kind of test is the cause of the destructive tests we found some time ago and we tried to workaround it here: #618

Could we follow the approach we are using in the base case for the Readme class ?. It can improve 3 aspects of these tests:

  • Fine tune control and always predictable active theme for test.
  • No destructive test env.
  • Avoid realying on CBT complex functions.

}

public function test_array_font_src() {
wp_set_current_user( self::$admin_id );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably not needed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without it, the tests break:

Time: 00:00.357, Memory: 42.50 MB

There was 1 error:

1) Test_Create_Block_Theme_Fonts::test_copy_activated_fonts_to_theme
Undefined array key "typography"

/var/www/html/wp-content/plugins/create-block-theme/tests/test-theme-fonts.php:43

ERRORS!
Tests: 67, Assertions: 163, Errors: 1.
Script phpunit handling the test event returned with error code 2
✖ Command failed with exit code 2
Command failed with exit code 2

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably we should use this refactor to improve the tests of this class. They currently relay on setting users, creating new themes dynamically and after removing it without restoring the previous state.

I agree that we should work on that, but since the tests currently cover the already-refactored code, I'd prefer refactoring them in another PR.

@vcanales vcanales force-pushed the try/code-readability-refactor branch from ded5ff5 to 2b409f8 Compare June 4, 2024 13:45
vcanales and others added 2 commits June 4, 2024 11:34
Copy link
Contributor

@pbking pbking left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems fine to me. Quality improvements.

@vcanales vcanales merged commit 8d65218 into trunk Jun 4, 2024
2 checks passed
@vcanales vcanales deleted the try/code-readability-refactor branch June 4, 2024 17:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants